/********************************************/
/* Mateusz Borkowski                        */
/********************************************/

#include "ShutdownDialog.h"
#include <QVBoxLayout>
#include <QLabel>
#include <QPushButton>
#include <QSettings>
#include <QDesktopWidget>
#include <QApplication>
#include <QFont>

#define WIN_SIZE_V 140
#define WIN_SIZE_H 200

ShutdownDialog::ShutdownDialog()
{
    QFont newFont("Courier", 18, QFont::Bold, false);
    QVBoxLayout *mainLayout = new QVBoxLayout(this);
    QVBoxLayout *buttonLayout = new QVBoxLayout;

    QLabel *label = new QLabel(tr("Are you sure?"));
    label->setFont(newFont);
    label->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
    QPushButton *cancelBtn = new QPushButton(tr("Cancel"));
    QPushButton *restartBtn = new QPushButton(tr("Restart"));
    QPushButton *shutdownBtn = new QPushButton(tr("Shutdown"));

    mainLayout->addWidget(label);
    mainLayout->addStretch();
    mainLayout->addLayout(buttonLayout);

    buttonLayout->addWidget(shutdownBtn);
    buttonLayout->addStretch();
    buttonLayout->addWidget(restartBtn);
    buttonLayout->addStretch();
    buttonLayout->addWidget(cancelBtn);

    shutdownBtn->setDefault(true);

    setWindowTitle(tr("Shutdown"));

    connect(cancelBtn, SIGNAL(clicked()), this, SLOT(reject()));
    connect(restartBtn, SIGNAL(clicked()), this, SLOT(rest()));
    connect(shutdownBtn, SIGNAL(clicked()), this, SLOT(shutdwn()));

    moveWindow();

}

void ShutdownDialog::rest()
{
    system("sudo /sbin/reboot;");
    this->accept();
}

void ShutdownDialog::shutdwn()
{
    system("sudo /sbin/shutdown -h now");
    this->accept();
}

void ShutdownDialog::moveWindow()
{
    QSettings settings("Mateusz Borkowski", "Shutdown2");
    QDesktopWidget *d = QApplication::desktop();
    int pos_x = (d->width() / 2) - (WIN_SIZE_H / 2);
    int pos_y = (d->height() / 2) - (WIN_SIZE_V / 2);
    QPoint pos = settings.value("pos", QPoint(pos_x, pos_y)).toPoint();
    QSize size = settings.value("size", QSize(WIN_SIZE_H, WIN_SIZE_V)).toSize();
    resize(size);
    move(pos);
}
